﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace XmlSerializer的使用
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //SerializeObject("123.config");
            OrderedItem item = XmlToFromFile("123.config", typeof(OrderedItem)) as OrderedItem;

            Console.WriteLine("item.Key = {0}，len={1}", item.Key, item.Key.Length.ToString());
            Console.WriteLine("item.Description = {0},len{1}", item.Description, item.Description.Length.ToString());
            Console.WriteLine("item.ItemName = {0}", item.ItemName);
            Console.WriteLine("item.Quantity = {0}", item.Quantity.ToString());
            Console.WriteLine("item.UnitPrice = {0}", item.UnitPrice.ToString());

            Console.WriteLine("请按任意键结束...");
            Console.ReadKey();
        }

        public static object XmlToFromFile(string filePath, Type type)
        {
            XmlSerializer xmlSerializer;
            FileStream fileStream = null;
            try
            {
                xmlSerializer = new XmlSerializer(type);
                using (fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    object objectFromXml = xmlSerializer.Deserialize(fileStream);
                    return objectFromXml;
                }
            }
            catch (Exception Ex)
            {
                fileStream.Close();
                //System.Windows.Forms.MessageBox.Show(string.Format("读取文件错误：{0}", filePath));
                Console.WriteLine(string.Format("读取文件错误：{0}", filePath));
                throw Ex;
            }
            finally
            {
                if (fileStream != null) fileStream.Close();
            }
        }


        public static void SerializeObject(string filename)
        {
            XmlSerializer serializer =
            new XmlSerializer(typeof(OrderedItem));

            // Create an instance of the class to be serialized.
            OrderedItem i = new OrderedItem();

            // Set the public property values.
            i.Key = "KeyFile";
            i.ItemName = "Widget";
            i.Description = "Regular Widget";
            i.Quantity = 10;
            i.UnitPrice = (decimal)2.30;

            // Writing the document requires a TextWriter.
            TextWriter writer = new StreamWriter(filename);

            // Serialize the object, and close the TextWriter.
            serializer.Serialize(writer, i);
            writer.Close();
        }

        public abstract class OrderedItemBase
        {
            public abstract string Key { get; set; }
        }

        // This is the class that will be serialized.
        // 文件中只有一部分的数据与这里的一部分一致时，只会对相同的赋值，其它的将被忽略
        public class OrderedItem:OrderedItemBase
        {
            public string ItemName;
            public string Description;
            public decimal UnitPrice;
            public int Quantity;

            protected string _key;
            public override string Key { get => _key; set => _key = value; }
        }

    }
}
